import { generateColumns, generateDictData } from '@/utils/plusUtil.js'
import { list, delLogininfor,  } from '@/api/monitor/logininfor'

export const useCommonConfig = ()=>{

  // 基础配置
  const { proxy } = getCurrentInstance()
  const primaryKey = 'infoId'
  const moduleName =  '登录日志'
  const apiFn = {
    listApiFn: list,
    delApiFn: delLogininfor,
  }

  // 删除前处理
  const beforeDelFn = (data)=>{
    return Array.isArray(data) ? data.map((item) => item[primaryKey]).join([',']) : data[primaryKey]
  }

  return {
    proxy,
    primaryKey,
    moduleName,
    apiFn,
    beforeDelFn,
  }

}


/**
 * 获取列和字典
 * @param isForm
 * @param data  表单数据对象
 */
export const useColumnsDict = (isForm = false, data) => {
  const { proxy } = getCurrentInstance()
  let formData = ref({})
  if (isForm) {
    if (!data || !isRef(data)) {
      console.error('当 isForm 为 true 时，data 必须是一个 ref 对象')
      return
    }
    formData = data
  }

  const dictRawData  = proxy.useDict(...[
    'sys_common_status',
  ])

  // 拓展了propInTable，propInForm
  // 定义在这个里面可以对外层进行覆盖(根据是列表还是表单)
  // orderInForm 可以调整在表单里面显示顺序
  const columnsData = [
    {
      label: "访问编号",
      prop: "infoId",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "用户账号",
      prop: "userName",
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "地址",
      prop: "ipaddr",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "登录地点",
      prop: "loginLocation",
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "操作系统",
      prop: "os",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "浏览器",
      prop: "browser",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "登录状态",
      prop: "status",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "描述",
      prop: "msg",
      hideInSearch: true,
      tableColumnProps: {
        align: "center"
      }
    },
    {
      label: "访问时间",
      prop: "loginTime",
      tableColumnProps: {
        align: "center"
      },
      valueType:'date-picker',
      fieldProps: {
        type: 'daterange',
        valueFormat: "YYYY-MM-DD"
      },
      render: val=> proxy.parseTime(val)
    },
  ]
  const columns = ref([])
  columns.value = generateColumns(columnsData, isForm)
  const dictData = computed(()=>generateDictData(dictRawData))
  return {
    dictData,
    columns,
  }
}

